//
//  BaseViewController.m
//  Learn
//
//  Created by icomexe on 2018/10/24.
//  Copyright © 2018年 cjg. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<UINavigationControllerDelegate>
/* <#描述#> */
@property (nonatomic, strong) NSMutableArray    * dataArray;

@end

@implementation BaseViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithObjects:
                      @"ReciteWordPlanViewController",@"EditPlanViewController",@"ReciteViewController",@"PkViewController",
                      nil];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    if ([self.dataArray containsObject:NSStringFromClass(self.class)]) {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }else {
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lWhiteColor] backgroundColor:nil shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:NO];

    NSLog(@"55555555%@",NSStringFromClass([self class]));
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if ([self.dataArray containsObject:NSStringFromClass(self.class)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }else {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEdit];
}
- (void)endEdit
{
    [self.view endEditing:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_APP_BACKGROUND;
    [self setBackButtonIsHidden:NO];
    self.navigationHeight = NavigationBarHeight;
    self.bottomSafeHeight = SafeAreaBottomHeight;
}
- (void)initCusNav
{
    self.navigationBar = [[BaseNavBar alloc]init];
    
    [self.view addSubview:self.navigationBar];

   WS(WeakSelf)
    self.navigationBar.leftClickedBlock = ^{
        [WeakSelf back];
    };
}

- (void)back {
    if (self.navigationController && self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded && !self.view.window) {
        self.view = nil;
    }
}

//隐藏导航栏

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSArray *vcArray = @[@"LearningRecordViewController",@"SchoolRecordViewController",@"QuestionFeedBackVC",@"AccountCancellationExplainVC",@"AccountCancellationVC",@"MyOrderController",@"PayDetailViewController",@"BookDetailViewController",@"AboutLearnController",@"MineViewController",@"SetViewController",@"VipCenterViewController",@"PersonInformationVC",@"CleanCacheViewController",@"NormalQuestionsViewController",@"ConnectCustomerViewController",@"LearnSettingViewController",@"MessageListViewController",@"MessageDetailViewController"];
    if ([vcArray containsObject:NSStringFromClass([viewController class])]) {
        [navigationController setNavigationBarHidden:NO animated:NO];
    }else{
        //[navigationController setNavigationBarHidden:YES animated:NO];
    }
   
}

//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    [navigationController setNavigationBarHidden:YES animated:NO];
//}


- (void)dealloc {
    NSLog(@"销毁:%@",NSStringFromClass([self class]));
}

/// alert提示操作【单个action】
- (void)alertViewTitle:(NSString*)title message:(NSString*)msg title:(NSString*)buttonTitle block:(KMBlock)block {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title?:@"" message:msg?:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:buttonTitle
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             if (block) {
                                                                 block();
                                                             }
                                                         }];
    [alertController addAction:cancelAction];
    [[self mainWindow].rootViewController presentViewController:alertController animated:YES completion:nil];
}

/// alert提示操作【左右双action】
- (void)alertViewTitle:(NSString*)title message:(NSString*)msg left:(NSString*)left leftBlock:(KMBlock)leftBlock right:(NSString*)right  rightBlock:(KMBlock)rightBlock  {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title?:@"" message:msg?:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:left?:@""
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             if (leftBlock) {
                                                                 leftBlock();
                                                             }
                                                         }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:right?:@""
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (rightBlock) {
                                                             rightBlock();
                                                         }
                                                     }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [[self mainWindow].rootViewController presentViewController:alertController animated:YES completion:nil];
}



-(void)showLoadingViewWithText:(NSString *)text{
    [self removeLoadingView];
    [self.view setUserInteractionEnabled:NO];
    self.objLoadingView=[[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.objLoadingView];
    self.objLoadingView.label.text=text;
    [self.objLoadingView showAnimated:YES];
}


-(void)removeLoadingView{
    [self.view setUserInteractionEnabled:YES];
    if(self.objLoadingView!=nil){
        [self.objLoadingView hideAnimated:YES];
        [self.objLoadingView removeFromSuperview];
        self.objLoadingView=nil;
    }
}


//获取当前的window
- (UIWindow *)mainWindow {
    UIApplication *app = [UIApplication sharedApplication];
    return [app.delegate window];
}

@end
